package ch04.game;

/**
 * @author chenmin
 * @desc TODO
 * @date 2025/9/3
 */


/**
 * 英雄类：继承Role，实现英雄专属逻辑（装备系统）
 */
public class Hero extends Role {

    private final Equip[] equips;
    private int equipCount;

    private static final int MAX_EQUIP = 3;


    public Hero(String name, int level, int blood, String kind, int basePower, int baseDefence) {
        super(name, level, blood, kind, basePower, baseDefence);
        this.equips = new Equip[MAX_EQUIP];
        this.equipCount = 0;
    }


    public void addEquip(Equip equip) {
        if (equip == null) {
            System.out.println("无法添加空装备！");
            return;
        }
        if (equipCount >= MAX_EQUIP) {
            System.out.printf("【%s】装备已满（最多%d个），无法添加【%s】！%n",
                    getName(), MAX_EQUIP, equip.getName());
            return;
        }

        for (int i = 0; i < equipCount; i++) {
            if (equips[i].getName().equals(equip.getName())) {
                System.out.printf("已装备【%s】，无需重复添加！%n", equip.getName());
                return;
            }
        }

        equips[equipCount++] = equip;
        System.out.printf("【%s】成功装备【%s】！%n", getName(), equip.getName());
    }


    @Override
    public int getTotalPower() {
        int equipPower = 0;
        for (int i = 0; i < equipCount; i++) {
            equipPower += equips[i].getPower();
        }
        return super.getBasePower() + equipPower;
    }


    @Override
    public int getTotalDefence() {
        int equipDefence = 0;
        for (int i = 0; i < equipCount; i++) {
            equipDefence += equips[i].getDefence();
        }
        return super.getBaseDefence() + equipDefence;
    }


    @Override
    public void attack(Role target) {

        if (this.isDead()) {
            System.out.printf("【%s】已死亡，无法攻击！%n", getName());
            return;
        }
        if (target.isDead()) {
            System.out.printf("【%s】已死亡，无需攻击！%n", target.getName());
            return;
        }


        int random = (int) (Math.random() * 10 + 1);
        boolean isCrit = random >= CRIT_THRESHOLD;
        int finalAttack = isCrit ? getTotalPower() * CRIT_MULTIPLE : getTotalPower();


        int damage = Math.max(MIN_DAMAGE, finalAttack - target.getTotalDefence());
        target.setBlood(target.getBlood() - damage);


        String critTip = isCrit ? "[暴击！]" : "";
        System.out.printf("【%s】%s攻击【%s】，造成%d点伤害！【%s】剩余血量：%d%n",
                getName(), critTip, target.getName(), damage, target.getName(), target.getBlood());
    }


    @Override
    public void showBaseInfo() {
        super.showBaseInfo();

        System.out.println("装备列表：");
        if (equipCount == 0) {
            System.out.println("  - 未装备任何装备");
            return;
        }
        for (int i = 0; i < equipCount; i++) {
            System.out.printf("  %d. 【%s】- 攻击力+%d | 防御力+%d%n",
                    i + 1, equips[i].getName(), equips[i].getPower(), equips[i].getDefence());
        }
    }
}